
let title = document.title
let host = location.host
console.info("捕获页面:: " + title + " | " + host)

injectCustomJs("plugs/pako.min.js")
injectCustomJs("plugs/crypto-js.min.js")
injectCustomJs("content/common.js")

let div = document.createElement("div")
let txt = document.createTextNode("小信")
div.appendChild(txt)
div.setAttribute("id", "ballId")
document.body.appendChild(div);
let callback = false;
var sieteMap = new Map()
var webSocket = false;
setTimeout(() => {
	if (host == "www.binance.com") {
		injectCustomJs("content/biance_content.js")
	}
	if (host == "599.com") {
		injectCustomJs("content/599_content.js")
	}
	if (title == "Welcome") {
		// injectCustomJs("content/hg_content.js")
		injectCustomJs("content/hg_bet_content.js")
	}
	if (host == "125.252.69.119") {
		// injectCustomJs("content/hg_content.js")
		//injectCustomJs("content/hg_bet_content.js") 
		injectCustomJs("content/hg_result.js")
	}
	if (host.indexOf("www.megadice.com") > -1) {
		injectCustomJs("content/megadice.js")
	}
	if (host.indexOf("old.megadice.com") > -1) {
		injectCustomJs("content/megadice_old.js")
	}
}, 1000);



window.addEventListener("message", function (e) {
	let site = e.data.site
	if (sieteMap.has(site)) {
		sieteMap.get(site)(e.data.data)
	}
	if (e.source !== window || !e.data.fromPage) return;
	// 转发给 service_worker
	chrome.runtime.sendMessage({
		from: "content_script",
		action: "page_message",
		data: e.data.payload
	}, (response) => {
		console.log("service_worker 返回：", response);
		// 继续转发回页面：
		window.postMessage({ fromContent: true, payload: response }, "*");
	});
}, false);


sieteMap.set("biance", function (data) {
	webSocket.send(JSON.stringify(data))
})


function injectCustomJs(jsPath) {
	if (!jsPath) return;
	var temp = document.createElement('script');
	temp.setAttribute('type', 'text/javascript');
	temp.src = chrome.runtime.getURL(jsPath);
	temp.onload = function () {
		this.parentNode.removeChild(this);
	};
	try {
		document.body.appendChild(temp);
	} catch (e) {
		console.info("加载js出错：" + jsPath)
	}
}

function injectCustomCss(cssPath) {
	if (!cssPath) return;
	var temp = document.createElement('style');
	temp.setAttribute('type', 'text/css');
	temp.src = chrome.runtime.getURL(cssPath);
	temp.onload = function () {
		this.parentNode.removeChild(this);
	};
	try {
		document.body.appendChild(temp);
	} catch (e) {
		console.info("加载js出错：" + jsPath)
	}
}

